package com.hzj.springcloud.controller;

import com.hzj.springcloud.pojo.Dept;
import com.hzj.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController//表示所有Controller接口返回字符串
public class DeptController {
    @Autowired
    DeptService ds;
    @Autowired
    DiscoveryClient discoveryClient;

    @PostMapping("/dept/add")
    //增加
    public String addDept(Dept dept) {
        int i = ds.addDept(dept);
        return String.valueOf(i);
    }

    @RequestMapping("/dept/list")
    //查询所有
    public List<Dept> show() {
        return ds.show();
    }

    @GetMapping("/dept/get/{id}")
    //根据id查询
    public Dept queryById(@PathVariable("id") int id) {
        return ds.queryById(id);
    }

    @RequestMapping("/dept/discoveryClient")
    public Object discoveryClient() {
        List<String> services = discoveryClient.getServices();
        System.out.println(services);//获取微服务列表清单，就是查看注册中心有哪些微服务
        //getInstances()方法值为：fangEureka网页中的application.name名
        List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROIVDER-DEPT-8001");
        for (ServiceInstance instance : instances) {//根据服务名获取具体微服务的信息
            System.out.println(instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId());
        }
        return discoveryClient;
    }
}
